function search(nums, target) {
  // 1. nums的长度为0，直接返回-1
  // 2. nums长度不为0
  //  2.1 找到了
  //  2.2 没找到
  if (nums.length === 0) {
    return -1;
  }
  let i = 0;
  let j = nums.length - 1;
  while (i <= j) {
    let k = Math.floor((i + j) / 2);
    if (nums[k] === target) {
      return k;
    } else if (nums[k] < target) {
      i = k + 1;
    } else {
      j = k - 1;
    }
  }
}

const nums = [-2, 3, 1, 5, 7, 9, 23];
const target = 7;

console.log(search(nums, 7));
